package com.tedu.controller;


import com.tedu.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@org.springframework.stereotype.Controller
public class Controller {
    /**
     * 使用request获取用户发送的数据
     * @param request
     * @return
     */
    @RequestMapping("/pamser01")
    @ResponseBody //直接返回文本数据，不需要进行视图解析，就不用加前缀和后缀这些
    public String paser01( HttpServletRequest request){

        return request.getParameter("name");
    }

    /**
     * 通过直接入参  获取用户发送的数据
     * @param name
     * @param pwd
     * @return
     */
    @RequestMapping("/pamser02")
    @ResponseBody //直接返回文本数据，不需要进行视图解析，就不用加前缀和后缀这些
    public String paser02(String name,String pwd){
        String pam = "用户名："+ name + ";密码：" + pwd;
        System.out.println(pam);
        return pam;
    }



    /**
     * 自动封装参数信息到bean
     * @param user  用户传入的参数
     * @return
     */
    @RequestMapping(value = "/login",produces = "text/html;charset=UTF-8")
    @ResponseBody //直接返回文本数据，不需要进行视图解析，就不用加前缀和后缀这些
    public String login(User user) {

        if (user.getUsername().equals("admin")&&user.getPassword().equals("123"))
        {
            return user.success();

        }else{
            return user.fail();
        }


    }

    @RequestMapping(value = "/register",produces = "text/html;charset=UTF-8")
    public void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("register.jsp");

    }

    @RequestMapping(value = "/registers",produces = "text/html;charset=UTF-8")
    public void registers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("success.jsp");
    }

}
